<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\setting\config;

use app\adminapi\services\setting\SystemConfigService;
use kitego\base\BaseController;
use think\App;
use think\response\Json;

class SystemConfigController extends BaseController
{
    public array $safeAction = ['getSysConfigByGuest'];

    private SystemConfigService $service;

    public function __construct(App $app, SystemConfigService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 获取系统非敏感配置项
     */
    public function getSysConfigByGuest(): Json
    {
        return $this->service->getSysConfigByGuest();
    }

    /**
     * 存储配置页面结构
     */
    public function schema()
    {
        $jsonFilePath = __DIR__ . '/' . 'configSchema.json';
        $jsonContent = file_get_contents($jsonFilePath);

        if ($jsonContent === false) exception('文件读取失败');

        return amis_success($jsonContent);
    }

    /**
     * 读取系统配置
     */
    public function listSystemConfig(): Json
    {
        $params = $this->params;
        $groupName = $params['groupName'] ?? null;
        $keys = !empty($params['keys']) ? $params['keys'] : null;

        return $this->service->listSystemConfig($groupName, $keys);
    }

    /**
     * 保存系统配置
     */
    public function editSystemConfig(): Json
    {
        $params = $this->params;
        if (empty($params['groupName']) || !is_array($params['data'])) exception('参数错误');

        return $this->service->editSystemConfig($params['groupName'], $params['data']);
    }
}